/**
 * 
 */
package com.magnusart.scrumify.dao;

import static org.junit.Assert.fail;

import java.util.List;

import org.junit.After;
import org.junit.Before;

import com.magnusart.scrumify.dao.CommonDAO;
import com.magnusart.scrumppm.model.Project;
import com.magnusart.scrumppm.model.ProjectBuilder;
import com.magnusart.scrumppm.model.Story;
import com.magnusart.scrumppm.model.StoryBuilder;
import com.magnusart.scrumppm.model.Task;
import com.magnusart.scrumppm.model.TaskBuilder;

/**
 * @author Magnus Andersson
 *
 */
public class TasksDAOActions_HsqlDb extends GenericDAOSpecification<Story, Task, TaskBuilder, CommonDAO<Story, Task, TaskBuilder> >
{
	private DBTestUtil.SeamedHsqlDbDAOFactory factory;
	private CommonDAO<Project, Project, ProjectBuilder> projectDao;
	private CommonDAO<Project,Story,StoryBuilder> storyDao;
	private Project storyParent;
	
	@Before
	public void setUp() throws Exception {

		DBTestUtil.setUpTempDatabase();
		
		factory = new DBTestUtil.SeamedHsqlDbDAOFactory();

		// Use our seamed instance with a special sql connection.
		dao = factory.getTaskDAO();
		projectDao = factory.getProjectDAO();
		storyDao = factory.getStoryDAO();
		List<Project> projs = projectDao.getAll( null );
		
		if( projs.size() > 0 )
			storyParent = projs.get( 0 );
		else
			fail( "No open parent project, needed to be able to test stories." );

		List<Story> stories = storyDao.getAll( storyParent );
		
		if( stories.size() > 0 ) 
			parent = stories.get( 0 );
		else
			fail( "No open parent stories, needed to be able to test tasks." );
		
		builder = new TaskBuilder();
		builder.setDescription( "A new task!" );
		builder.setOrder( 0 );
		
		entry1 = new Task( builder );
		entry2 = new Task( builder );
		
		builder = new TaskBuilder();
		builder.setDescription( "Yet another task!");
		builder.setOrder( 1 );
		
		entry3 = new Task( builder );
		
		builder.setDescription(" Entry3 Desc Updated");
	}
	

	@After
	public void tearDown() throws Exception {
		DBTestUtil.closeConn();
	}


	@Override
	boolean deepEquality( Task a, TaskBuilder b) {
			Task c = new Task( b );
			
			return a.getID() == c.getID() && compareWithoutID( a, c );
	}


	@Override
	boolean compareWithoutID( Task a, Task c ) {
		return a.getDescription().equals( c.getDescription()) &&
		   a.getOrder() == c.getOrder() &&
		   a.isDone() == c.isDone();
	}
	
}
